<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\AdminController as Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request; 
use App\Model\{Admin,User,Config,Cat,Article}; 
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
use App\Libraries\vendor\Sms;
use GeoIp2\Database\Reader;
use App\Libraries\vendor\SphinxClient;

/**
 * @menu_cat 合伙人投稿
 * @icon cog
 * @sort 30
 */
class TouController extends Controller
{
	public function __invoke(){
        return $this->index(func_get_args());
    }

    /**
     * @menu 投稿待审核
     * @return [type] [description]
     */
    public function index(){
    	$list = DB::table('advertorials')->where('examine',0)->where('status','<>',2);
        $this->_search($list, $search);
        $total = $list->count();
        $this->_order($list, ['id'=>'desc']);
        $list = $list->paginate(20);
       	
       	foreach($list as &$item) {
       		$item->pic = explode(',',$item->pic);
       	}

        return view('Admin.Tou.index')->with([
            'list' => $list,
            '_search'=>$search,
            'status'=>Config::get_config_bykey('advertorials_status'),
            'examine'=>Config::get_config_bykey('advertorials_examine'),
        ]);
    }

    /**
     * @menu 通过列表
     * @return [type] [description]
     */
    public function allow(){
    	$list = DB::table('advertorials')->where('examine',1)->where('status',0);
        $this->_search($list, $search);
        $total = $list->count();
        $this->_order($list, ['id'=>'desc']);
        $list = $list->paginate(20);
       	
       	foreach($list as &$item) {
       		$item->pic = explode(',',$item->pic);
       	}

        return view('Admin.Tou.allow')->with([
            'list' => $list,
            '_search'=>$search,
            'status'=>Config::get_config_bykey('advertorials_status'),
            'examine'=>Config::get_config_bykey('advertorials_examine'),
        ]);
    }
	
	/**
     * @menu 线上列表
     * @return [type] [description]
     */
    public function line(){
    	$list = DB::table('advertorials')->where('examine',1)->where('status',1);
        $this->_search($list, $search);
        $total = $list->count();
        $this->_order($list, ['id'=>'desc']);
        $list = $list->paginate(20);
       	
       	foreach($list as &$item) {
       		$item->pic = explode(',',$item->pic);
       	}

        return view('Admin.Tou.line')->with([
            'list' => $list,
            '_search'=>$search,
            'status'=>Config::get_config_bykey('advertorials_status'),
            'examine'=>Config::get_config_bykey('advertorials_examine'),
        ]);
    }


    /**
     * @menu 已拒绝
     * @return [type] [description]
     */
    public function refund(){
    	$list = DB::table('advertorials')->where('examine',2)->where('status','<>',2);
        $this->_search($list, $search);
        $total = $list->count();
        $this->_order($list, ['id'=>'desc']);
        $list = $list->paginate(20);
       	
       	foreach($list as &$item) {
       		$item->pic = explode(',',$item->pic);
       	}

        return view('Admin.Tou.refund')->with([
            'list' => $list,
            '_search'=>$search,
            'status'=>Config::get_config_bykey('advertorials_status'),
            'examine'=>Config::get_config_bykey('advertorials_examine'),
        ]);
    }

    /**
     * [ajax_info description]
     * @return [type] [description]
     */
    public function ajax_info($id){
    	if(request()->isMethod('post')){
    		$param = request()->only(['examine', 'examine_message']);
    		DB::table('advertorials')->where('id',$id)->update([
    			'examine'=>$param['examine'],
    			'examine_message'=>$param['examine_message'],
                'examine_time'=>time(),
    		]);
    	
    	}

    	$data= DB::table('advertorials')->where('id',$id)->first();
    	$rs = [
    		'data'=>$data,
    	];
 
	 	return view('Admin.Tou.info')->with($rs);
    }

    public function ajax_update_status($id){
    	$param = request()->only(['status']);


    	$data = [
    		'status'=>$param['status'],
    	];
    	if($param['status']==1){
    		$data['examine'] = 1;
    	}

    	DB::table('advertorials')->where('id',$id)->update($data);
    	return back();
    }
}